package com.system.dao.impl;


import com.system.config.Jdbc;
import com.system.dao.TopicData;
import com.system.untils.FormatOutput;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class TopicDataMapper implements TopicData {
    private final Jdbc jb;
    private final FormatOutput fo;

    public TopicDataMapper() {
        this.jb = new Jdbc();
        this.fo = new FormatOutput();
    }

    @Override
    public void selectTopicsForUser(String email) {
        try {
            String sql = "select * from publish_tb where email=?";
            PreparedStatement pstmt = this.jb.prepareStatement(sql);
            pstmt.setString(1, email);
            ResultSet rs = pstmt.executeQuery();
            int index = 0;
            while (rs.next()) {
                String text = rs.getString("text");
                if (text.length() > 20) {
                    if (rs.getInt("isShow") == 1) {
                        System.out.println(" " + (++index) + " ¦ " + " " + fo.formatStringByLength(text, 20) + "\t-->已隐藏");
                        System.out.println("------------------------------------------------");
                    } else {
                        System.out.println(" " + (++index) + " ¦ " + " " + fo.formatStringByLength(text, 20));
                        System.out.println("------------------------------------------------");
                    }
                } else {
                    if (rs.getInt("isShow") == 1) {
                        System.out.println(" " + (++index) + " ¦ " + " " + text + "\t-->已隐藏");
                        System.out.println("------------------------------------------------");
                    } else {
                        System.out.println(" " + (++index) + " ¦ " + " " + text);
                        System.out.println("------------------------------------------------");
                    }

                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void selectAllTopics(int currentPage) {
        try {
            int pageSize = 5;
            int offset = (currentPage - 1) * pageSize;  // 计算查询的偏移量

            String sql = "SELECT * FROM publish_tb WHERE isShow = 0 LIMIT ? OFFSET ?";
            PreparedStatement pstmt = jb.prepareStatement(sql);
            pstmt.setInt(1, pageSize);  // 每页显示 5 条
            pstmt.setInt(2, offset);    // 偏移量

            ResultSet rs = pstmt.executeQuery();
            int index = offset;
            while (rs.next()) {
                String name = rs.getString("name");
                String text = rs.getString("text");
                // 根据 text 长度显示内容
                if (text.length() > 20) {
                    System.out.println(" " + (++index) + " ¦ " + name+  ": ");
                    System.out.println(" "+fo.formatStringByLength(text, 20));
                    System.out.println("------------------------------------------------");
                } else {
                    System.out.println(" " + (++index) + " ¦ " + name+  ": ");
                    System.out.println(" "+text);
                    System.out.println("------------------------------------------------");
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    // hashmap 集合存储对应的信息用户
    @Override
    public HashMap<Integer, Integer> selectAllIdForUser(String email) {
        try {
            HashMap<Integer, Integer> map = new HashMap<>();
            String sql = "select * from publish_tb where email=?";
            PreparedStatement pstmt = this.jb.prepareStatement(sql);
            pstmt.setString(1, email);
            ResultSet rs = pstmt.executeQuery();
            int index = 0;
            while (rs.next()) {
                int id = rs.getInt("id");
                map.put(++index, id);
            }
            return map;
        } catch (SQLException e) {
            return null;
        }
    }

    // 获得总数
    @Override
    public int getTotalRecords() {
        int totalRecords = 0;
        try {
            String sql = "SELECT COUNT(*) FROM publish_tb WHERE isShow = 0";
            PreparedStatement pstmt = jb.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();

            // 处理结果集
            if (rs.next()) {
                totalRecords = rs.getInt(1); // 获取查询结果中的第一列，即记录数
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return totalRecords;
    }

    // 隐藏帖子 先要判断isShow 是不是已经隐藏
    @Override
    public boolean ifShowForUser(int id, String email) {
        try {
            HashMap<Integer, Integer> map = selectAllIdForUser(email);
            String sql1 = "select isShow from publish_tb where email=? and id=?";
            PreparedStatement pstmt = this.jb.prepareStatement(sql1);
            pstmt.setString(1, email);
            pstmt.setInt(2, map.get(id));
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                if (rs.getInt("isShow") == 1) {
                    return false;
                } else {
                    map.put(0, 0);
                    String sql2 = "update publish_tb set isShow = 1 where id = ?";
                    pstmt = this.jb.prepareStatement(sql2);
                    pstmt.setInt(1, map.get(id));
                    pstmt.execute();
                    System.out.println("隐藏成功！");
                    return true;
                }
            } else {
                System.out.println("no data");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);

        }
        return false;
    }
}
